<?php

namespace app\index\controller;

use think\Request;

use think\Db;
use think\Validate;
class Ucenter extends IndexController
{
    /**
     * æ˜¾ç¤ºä¸ªäººä¸­å¿ƒ
     *
     * @return \think\Response
     */
    public function index()
    {
        //æŸ¥è¯¢è®¢å•
        $order = Db::name('myclass')->field('price,createtime,cname,status')->where('uid',session('web.uid'))->select();
        return view('ucenter/index',['order'=>$order]);
    }

    /**
     * æ˜¾ç¤ºä¸ªäººèµ„æ–™é¡?
     *
     * @return \think\Response
     */
    public function UcenterInfo()
    {
        $info = Db::name('web_users')->where('id',session('web.uid'))->find();
        return view('ucenter/information',['info'=>$info]);
    }

    public function edit()
    {
        $data = input('post.');

        $file = request()->file('face');
        // dump($file);die;
        if(empty($file)){
            // $address = null;
            unset($data['face']);
        }else{
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            // dump($info);die;
            if($info){
                
                $address = $info->getSaveName();
                
                $address = str_replace('\\','/',$address);
                // dump($address);die;
                $data['face'] = $address;
            }else{
                //上传失败获取错误信息
                echo $file->getError();
            }

        }

        
        
        
        $tel = $data['tel'];
        $teldup = Db::name('web_users')->where('tel',$tel)->where('id','neq',session('web.id'))->select();
        if(!empty($teldup)){
            $this->error('电话号码已存在');
        }
        //验证数据
        $validate = new Validate(
            [
                'nickname' => 'require|regex:/^[_0-9a-zA-Z]{4,16}$/',
                'name' => 'regex:/^[\x{4e00}-\x{9fa5}]{2,16}$/u',
                'tel' => "require|regex:/^1[34578]\d{9}$/",
                'email' => "require|regex:/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/",
                'pass' => 'max:16|regex:/^[_0-9a-zA-Z_]{4,16}$',
                'repass' => 'max:16|confirm:pass',
            ],
            [
                'nickname.require' => '昵称必须填',
                'nickname.regex'     => '昵称为-16位数字字母或者下划线',
                'name.regex'=>'真实姓名为2-16位中文',
                'tel.require' => '手机号码不能为空,请重试...',
                'tel.regex' => '手机号码必须符合规范,请重试...',
                'email.require' => '邮箱不能为空,请重试...',
                'email.regex' => '邮箱必须符合规范,请重试...',
                'pass.max' => '密码最多只能16位,包含数字字母下划线,请重试...',
                'repass.confirm' => '两次密码不一致,请重试...',

            ]);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        
        if(empty($data['pass'])){
            unset($data['pass']);
            unset($data['repass']);
        }else{

            // 删除repass
            unset($data['repass']);
            
            $data['pass'] = sha1($data['pass']);
        }
        // var_dump($data); die;
        // 添加用户
        // 开启事务
        $success = null;
        Db::startTrans();

        try{
            $add = Db::name('web_users')
                ->where('id',session('web.uid'))
                ->update($data);
                // dump($add);die;
            if($add){
                Db::commit();
                if(!empty($data['face'])){
                    session('web.face', $data['face'], 'think');
                }
                
                session('web.tel', $data['tel'], 'think');
                session('web.nickname', $data['nickname'], 'think');

                $this->success('更改成功');
            }else{
                $this->error('暂无更新');
            }

        }catch(Exception $exception){
            Db::rollback();

            $this->error('由于未知错误,添加失败,请重试...');
        }
    
    }

    public function myorder()
    {
        $order = Db::name('myclass')->where('uid',session('web.uid'))->select();
        return view('ucenter/order',['order'=>$order]);
    }

    /**
     * ä¿å­˜æ–°å»ºçš„èµ„æº?
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    
    //手机端个人中心
    public function mobile_person()
    {
        $info = Db::name('web_users')->where('id',session('web.uid'))->find();
        return view('ucenter/mobile_person',['info'=>$info]);
    }

   
}
